package com.nwcl.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author liguiqin
 * @date 2020/9/4
 */
public class DateUtil {

    public static Date getMaxDate(){
        return DateUtil.parseDateTime("2099-12-31 23:59:59");
    }


    public static boolean isBetweenTime(Date startTime, Date endTime, Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        // 设置开始时间
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startTime);
        // 设置结束时间
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endTime);
        // 检查时间是否在开始和结束时间之间
        return calendar.after(startCalendar) && calendar.before(endCalendar);
    }


    public static String formatDateDay(Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }

    public static String formatDateTime(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatDate(Date date, String formatStr) {
        return new SimpleDateFormat(formatStr).format(date);
    }

    public static String formatTime(Date date) {
        return new SimpleDateFormat("HH:mm").format(date);
    }

    public static Date parseDateTime(String timeString) {
        try {
            return DateUtils.parseDate(timeString,
                    new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-M-d H:m:s",
                            "yyyy-MM-dd H:m:s", "yyyy-M-d HH:mm:ss", "yyyyMMddHHmmss"});
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date parseDate(String dateString) {
        try {
            return DateUtils.parseDate(dateString,
                    new String[]{"yyyy-MM-dd", "yyyy-MM", "yyyy-M-d", "yyyy-MM-d", "yyyy-M-dd"});
        } catch (ParseException e) {
            return null;
        }
    }


    public static Date getDateStart(Date date) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public static Date getDateEnd(String date) {
        if (StringUtils.isBlank(date)) {
            return null;
        }
        Date d = parseDate(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    public static int getDiffDays(Date beginDate, Date endDate) {

        if (beginDate == null || endDate == null) {
            throw new IllegalArgumentException("getDiffDays param is null!");
        }
        long diff = (endDate.getTime() - beginDate.getTime())
                / (1000 * 60 * 60 * 24);

        int days = new Long(diff).intValue();

        return days;
    }

    //返回某个日期下几天的日期
    public static Date getNextDay(Date date, int i) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
        return cal.getTime();
    }

    //返回某个日期前几天的日期
    public static Date getFrontDay(Date date, int i) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
        return cal.getTime();
    }

}
